﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.Ads;
using Bussiness.Ads;
using Bussiness.Language;
using FOModels.Language;
namespace FO.Controllers
{
    public class AdsController : Controller
    {
        //
        // GET: /Ads/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AdsInternetBankingLeft()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int AdsPageInternetBankingID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["AdsPageInternetBankingID"].ToString());
            AdsBLL adsbll = new AdsBLL();
            AdsModels ads = new AdsModels();
            ads = adsbll.getAdsByPageID(AdsPageInternetBankingID, 2, lang.LangID);
            return PartialView(ads);
        }
        public ActionResult AdsInternetBankingRight()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int AdsPageInternetBankingID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["AdsPageInternetBankingID"].ToString());
            AdsBLL adsbll = new AdsBLL();
            AdsModels ads = new AdsModels();
            ads = adsbll.getAdsByPageID(AdsPageInternetBankingID, 3, lang.LangID);
            return PartialView(ads);
        }
        public ActionResult AdsMobileLeft()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int AdsPageMobileBankingID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["AdsPageMobileBankingID"].ToString());
            AdsBLL adsbll = new AdsBLL();
            AdsModels ads = new AdsModels();
            ads = adsbll.getAdsByPageID(AdsPageMobileBankingID, 2, lang.LangID);
            return PartialView(ads);
        }
        public ActionResult AdsMobileRight()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            int AdsPageMobileBankingID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["AdsPageMobileBankingID"].ToString());
            AdsBLL adsbll = new AdsBLL();
            AdsModels ads = new AdsModels();
            ads = adsbll.getAdsByPageID(AdsPageMobileBankingID, 3, lang.LangID);
            return PartialView(ads);
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerAds()
        {
            return View();
        }
        public JsonResult getAdsJson()
        {
            AdsBLL adsbll = new AdsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<AdsModels> list = new List<AdsModels>();
            list = adsbll.getAdsToManager(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertAds()
        {
            AdsBLL adsbll = new AdsBLL();
            List<AdsPositionModels> position = new List<AdsPositionModels>();
            List<AdsPageModels> page = new List<AdsPageModels>();
            position = adsbll.getAdsPosition();
            page = adsbll.getAdsPage();
            ViewBag.PageList = new SelectList(page, "AdsPageID", "AdsPageName");
            ViewBag.PositionList = new SelectList(position, "AdsPositionID", "AdsPositionName");
            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertAds(FO.Models.AdsInsert ads)
        {
            AdsBLL adsbll = new AdsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<AdsPositionModels> position = new List<AdsPositionModels>();
            List<AdsPageModels> page = new List<AdsPageModels>();
            position = adsbll.getAdsPosition();
            page = adsbll.getAdsPage();
            ViewBag.PageList = new SelectList(page, "AdsPageID", "AdsPageName");
            ViewBag.PositionList = new SelectList(position, "AdsPositionID", "AdsPositionName");
            if (ModelState.IsValid)
            {
                AdsModels item = new AdsModels();
                AdsModels itemLang = new AdsModels();
                item.AdsName = ads.AdsName;
                item.AdsPageID = ads.AdsPageID;
                item.AdsPositionID = ads.AdsPositionID;
                item.AdsActive = false;
                item.AdsUrlImgInfo = ads.AdsUrlImgInfo;
                item.AdsImg = ads.AdsImg;
                item.AdsImgInfo = ads.AdsImgInfo;

                itemLang.AdsName = ads.AdsNameLang;
                itemLang.AdsPageID = ads.AdsPageID;
                itemLang.AdsPositionID = ads.AdsPositionID;
                itemLang.AdsActive = false;
                itemLang.AdsUrlImgInfo = ads.AdsUrlImgInfo;
                itemLang.AdsImg = ads.AdsImgLang;
                itemLang.AdsImgInfo = ads.AdsImgInfoLang;
                adsbll.InsertAds(item);
                AdsModels adsitem = adsbll.getTop1Ads();

                item.AdsID = adsitem.AdsID;
                itemLang.AdsID = adsitem.AdsID;

                adsbll.InsertAdsLang(item,1);
                adsbll.InsertAdsLang(itemLang, 2);
                return RedirectToAction("ManagerAds");

            }
            return View(ads);

        }


        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateAds(int AdsID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            AdsBLL adsbll = new AdsBLL();
            List<AdsPageModels> page = new List<AdsPageModels>();
            List<AdsPositionModels> position = new List<AdsPositionModels>();
            AdsModels ads = new AdsModels();
            page = adsbll.getAdsPage();
            position = adsbll.getAdsPosition();
            ads = adsbll.getAdsByIDToManager(AdsID,lang.LangID);
            Models.Ads c = new Models.Ads();
            ViewBag.PageList = new SelectList(page, "AdsPageID", "AdsPageName", ads.AdsPageID);
            ViewBag.PositionList = new SelectList(position, "AdsPositionID", "AdsPositionName", ads.AdsPositionID);
            c.AdsID = ads.AdsID;
            c.AdsImg = ads.AdsImg;
            c.AdsImgInfo = ads.AdsImgInfo;
            c.AdsName = ads.AdsName;
            c.AdsActive = ads.AdsActive;
            c.AdsPageID = ads.AdsPageID;
            c.AdsPositionID = ads.AdsPositionID;
            c.AdsUrlImgInfo = ads.AdsUrlImgInfo;
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateAds(FO.Models.Ads ads)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            AdsBLL adsbll = new AdsBLL();
            List<AdsPageModels> page = new List<AdsPageModels>();
            List<AdsPositionModels> position = new List<AdsPositionModels>();
            AdsModels ca = new AdsModels();
            page = adsbll.getAdsPage();
            position = adsbll.getAdsPosition();
            ca = adsbll.getAdsByIDToManager(ads.AdsID,lang.LangID);
            ViewBag.PageList = new SelectList(page, "AdsPageID", "AdsPageName",ca.AdsPageID);
            ViewBag.PositionList = new SelectList(position, "AdsPositionID", "AdsPositionName",ca.AdsPositionID);
            if (ca != null)
            {
                ca.AdsID = ads.AdsID;
                ca.AdsImg = ads.AdsImg;
                ca.AdsImgInfo = ads.AdsImgInfo;
                ca.AdsName = ads.AdsName;
                ca.AdsActive = ads.AdsActive;
                ca.AdsPageID = ads.AdsPageID;
                ca.AdsPositionID = ads.AdsPositionID;
                ca.AdsUrlImgInfo = ads.AdsUrlImgInfo;
                adsbll.UpdateAds(ca, lang.LangID);

                return RedirectToAction("ManagerAds");
            }
            return View();


        }


        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteAds(int AdsID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            AdsBLL bll = new AdsBLL();
            AdsModels ads = new AdsModels();
            ads = bll.getAdsByIDToManager(AdsID,lang.LangID);
            if (ads != null)
            {
                bll.DeleteAds(ads.AdsID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }

        }
    }
}
